package cn.py.boot_lua.service.asynser;

import cn.py.boot_lua.asyn.AsyncTaskFactory;
import cn.py.boot_lua.asyn.updateAsyncMeth.AsyncTaskManager;
import cn.py.boot_lua.util.exception.BusinessException;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.function.Consumer;

@Slf4j
@Service
public class AsynserviceImpl {
    private final AsyncTaskManager taskManager;

    private static final String VIDEO_DUBBING_TASK_QUEUE = "video_dubbing_task_queue";
    private static final int MAX_CONCURRENCY = 1;
    private static final int QUEUE_CAPACITY = 100;
    private final ThreadPoolTaskExecutor executor;
    public AsynserviceImpl(AsyncTaskManager taskManager, @Qualifier("defaultThreadPoolTaskExecutor") ThreadPoolTaskExecutor executor){
        this.taskManager = taskManager;
        this.executor = executor;
    }

    public void submitVideoDubbingTask(JSONObject task) {
        log.info("提交视频配音任务到异步队列: "+ task);
        AsyncTaskFactory.submit(
                VIDEO_DUBBING_TASK_QUEUE,     // 任务队列名
                task,                         // 任务对象
                executor,                     // 执行任务的线程池
                MAX_CONCURRENCY,              // 最大并发数
                this::processTask,            // 任务处理器（lambda指向实际处理方法）
                QUEUE_CAPACITY                // 队列容量
        );
    }

    public void submitVideoDubbingTask101(JSONObject task) {
        Consumer<Exception> errorHandler = e -> {
            log.error("异步任务执行失败的原因: {}", e.getMessage());
        };
        boolean taskExecutor = taskManager.submit(
                VIDEO_DUBBING_TASK_QUEUE,
                task,
                "defaultThreadPoolTaskExecutor",
                MAX_CONCURRENCY,
                this::processTask,
                errorHandler,
                10
        );
        if (taskExecutor) {
            log.info("异步任务提交成功");
        }else {
            log.info("异步任务提交失败");
        }
    }



    private void processTask(JSONObject task) {
        String uid = task.getString("uid");
        String key = task.getString("key");
        try {
            processDataAsync(uid,key);
        } catch (Exception e) {
            log.error("任务处理失败: {}", task, e);
            throw new BusinessException("任务处理失败: " + e.getMessage());
        }
    }
    private void processDataAsync(String uid, String apiKey) {
        try {
            log.info("id: " + uid);
            log.info("ken: " + apiKey);
            int sun = Integer.parseInt(apiKey) / Integer.parseInt(uid);
            log.info("SUN结果为:" + sun);
            log.info("--------------------------------------结束------------------------------------------");
        }catch (NumberFormatException e) {
            throw new NumberFormatException("参数不合法！");
        } catch (ArithmeticException e) {
            throw new ArithmeticException("错误：除数不能为零！");
        } catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }
}
